package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils.TruncateAt;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.JianGuanDuiXiangListAdapter2;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.adapter.dao.DatabaseHelper;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.BeiJianDanWei;
import com.cz.nongyetongv5.bean.JianGuanDuiXiangFilter;
import com.cz.nongyetongv5.bean.Town;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.custom.dialog.CustomDialog;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.ConfigUtil;
import com.cz.nongyetongv5.utils.GoHomeUtil;
import com.cz.nongyetongv5.utils.HttpUtils;
import com.cz.nongyetongv5.utils.HttpUtils.OnRequestExecuted;
import com.cz.nongyetongv5.utils.UpdateDateUtilEx;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/***
 import com.amap.api.location.AMapLocation;
 import com.amap.api.location.AMapLocationListener;
 import com.amap.api.location.LocationManagerProxy;
 import com.amap.api.location.LocationProviderProxy;
 ***/
/**
 * xrf
 * public class JianGuanDuiXiangActivity extends Activity implements
 OnClickListener, OnItemClickListener, AMapLocationListener {
 * */
public class JianGuanDuiXiangActivity extends Activity implements
		OnClickListener, OnItemClickListener {

	private User user = null;

	private ViewGroup btnBack = null;

	/**
	 * 数据库工具类
	 */
	private DatabaseHelper db = null;

	/**
	 * 导航的数据
	 */
	private List<Town> navgationDatas = new ArrayList<Town>();

	private ViewGroup navgationLayout = null;

	private ListView lvDuiXiang = null;

	private JianGuanDuiXiangListAdapter2 adpJianGuanDuiXiang = null;

	private ViewGroup btnFilter = null;

	public static final int REQUEST_FILTER_CODE = 1;

	private JianGuanDuiXiangFilter filter = new JianGuanDuiXiangFilter();

	private EditText edtFilterLinkMan = null;
	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
									  int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			page=0;
		}
	};
	/**xrf
	 * 高德地图的定位属性

	 private LocationManagerProxy mAMapLocManager = null;

	 * 高德地图的定位信息对象
	 private AMapLocation location = null;
	 */
	private Button btnLeft;

	private Button btnRight;

	private View btn_search;

	private View page_left;
	private View page_right;
	private int page=0;
	private boolean isInit=true;

	private boolean hasNoData=false;

	public class PageListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if(arg0.getId()==R.id.pageLeft){
				//上一页
//				page--;
//				if(page<0)page=0;

				if(page==0){
					Toast.makeText(JianGuanDuiXiangActivity.this,"已是第一页",Toast.LENGTH_SHORT).show();
					return;
				}else{
					page--;
					hasNoData=false;
				}
			}else if(arg0.getId()==R.id.pageRight){
				//下一页
//				page++;
				if(hasNoData){
					Toast.makeText(JianGuanDuiXiangActivity.this,"没有更多数据",Toast.LENGTH_SHORT).show();
					return;
				}else {
					page++;
				}
			}

			if (edtFilterLinkMan.getText().length() > 0) {
				Log.i("test","1");
				refreshDataSelect(edtFilterLinkMan.getText().toString());
			} else {
				Log.i("test","2");
				refreshData(activeTown);
			}

		}


	}
	private  Handler  mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 12:
					List datas=(List)msg.obj;
					if(datas!=null){
						adpJianGuanDuiXiang.setData(datas);
						adpJianGuanDuiXiang.notifyDataSetChanged();

						if (datas.size() < 1) {
							hasNoData=true;
							Toast.makeText(JianGuanDuiXiangActivity.this,
									"没有更多数据 ", Toast.LENGTH_SHORT).show();
						}
					}

			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.jianguanduixiang);

		navgationLayout = (ViewGroup) findViewById(R.id.llNavgationLayout);

		edtFilterLinkMan = (EditText) findViewById(R.id.edtFilterLinkMan);
		edtFilterLinkMan.clearFocus();
		edtFilterLinkMan.addTextChangedListener(watcher);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		btn_search=findViewById(R.id.btn_search);
		page_left=findViewById(R.id.pageLeft);
		page_right=findViewById(R.id.pageRight);
		page_left.setOnClickListener(new PageListener());
		page_right.setOnClickListener(new PageListener());
		btn_search.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (edtFilterLinkMan.getText().length() > 0) {
					//callSearch
					refreshDataSelect(edtFilterLinkMan.getText().toString());
				} else {
					if (navgationLayout != null) {
						navgationLayout.setVisibility(View.VISIBLE);
					}
					page=0;
					initNav();
					notifyDatasetNavgation();
					refreshData(getCurrentTown());
				}

			}});


		// 查询出数据库对象
		db = new DatabaseHelper(this, CommonConstants.DBNAME, null,
				CommonConstants.DATABASE_VERSION);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		user = (User) getIntent().getSerializableExtra("user");

		if (user.getKnowRegion() != null) {

			String code = user.getKnowRegion();

			while (true) {

				Town town = db.queryTownById(code);

				if (town.getCode() == user.getProvinceId()) {
					break;
				}

				navgationDatas.add(0, town);

				code = town.getParent_code();

				if (user.getTown_path_id().length() > 3 && "32".equals(code)) {

					break;
				}

				if (code == null) {
					break;
				}

			}

			notifyDatasetNavgation();

		}

		lvDuiXiang = (ListView) findViewById(R.id.lvDuiXiang);
		lvDuiXiang.setOnItemClickListener(this);

		//refreshData(getCurrentTown());

		//btnFilter = (ViewGroup) findViewById(R.id.btnFilter);

		//btnFilter.setOnClickListener(this);

		btnLeft = (Button) findViewById(R.id.btnLeft);
		btnLeft.setOnClickListener(this);

		btnRight = (Button) findViewById(R.id.btnRight);
		btnRight.setOnClickListener(this);

		if (CommonConstants.isNetworkConnected(this)) {

			findViewById(R.id.llBar).setVisibility(View.VISIBLE);
//			activeTown=getCurrentTown();
			onClick(btnRight);

		} else {
			findViewById(R.id.llBar).setVisibility(View.GONE);
			activeTown=getCurrentTown();
			refreshData(getCurrentTown());
		}

		loadRightTitle();
	}
	private void initNav(){
		navgationDatas = new ArrayList<Town>();
		if (user.getKnowRegion() != null) {

			String code = user.getKnowRegion();

			while (true) {

				Town town = db.queryTownById(code);

				if (town.getCode() == user.getProvinceId()) {
					break;
				}

				navgationDatas.add(0, town);

				code = town.getParent_code();

				if (user.getTown_path_id().length() > 3 && "32".equals(code)) {

					break;
				}

				if (code == null) {
					break;
				}

			}

			notifyDatasetNavgation();

		}
	}
	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {
					GoHomeUtil.goHome(getApplicationContext());
				}
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

	/**
	 * 清理高德地图的资源
	 */
	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);
		/**xrf
		 if (mAMapLocManager != null) {
		 mAMapLocManager.removeUpdates(this);
		 mAMapLocManager.destory();
		 }
		 mAMapLocManager = null;
		 */
		super.onDestroy();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == REQUEST_FILTER_CODE) {

			if (data != null) {
				filter = (JianGuanDuiXiangFilter) data
						.getSerializableExtra("filter");

				refreshData(getCurrentTown());

				if (navgationLayout != null) {
					navgationLayout.setVisibility(View.VISIBLE);
				}

			}

		}

	}
	public static  int type=2;
	private int alarmSum=0;
	private List<String> alarmCodes=new ArrayList<String>();
	@Override
	public void onClick(View v) {

		if (v == btnLeft) {
			initNav();
			alarmCodes=new ArrayList<String>();
			edtFilterLinkMan.setText("");
			page=0;
			btnLeft.setBackgroundResource(R.drawable.btn_back_pressed);
			btnRight.setBackgroundResource(R.drawable.btn_back_pressed);
			type=1;
			btnLeft.setBackgroundColor(getResources().getColor(
					R.color.title_color));

			new AsyncTask<Integer, Integer, List<String>>() {

				private CustomDialog customDialog = null;

				@Override
				protected List<String> doInBackground(Integer... params) {

					final List<String> ids = new ArrayList<String>();

					String requestUrl = new ConfigUtil(getResources())
							.getPreHttpUrl()
							+ getString(R.string.jianguanduixiang_filter_url);

					String validateUsername = new ConfigUtil(getResources())
							.getRestUsername();

					String validatePassword = new ConfigUtil(getResources())
							.getRestPassword();

					HttpUtils httpUtils = HttpUtils.getInstance();

					httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

						@Override
						public void onRequestExecuted(int responseCode,
													  String responseBody) {
							try {
								JSONObject jsonObject = new JSONObject(
										responseBody);
								Log.d("rafealarm",String.valueOf(jsonObject));
								String status = jsonObject.getString("status");

								if ("1".equals(status) || "true".equals(status)) {
									alarmSum=jsonObject.optInt("total",0);
									JSONArray jsonArray = jsonObject
											.getJSONArray("result");
									Log.d("rafealarm",String.valueOf(jsonArray.length()));
									for (int i = 0; i < jsonArray.length(); i++) {
										JSONObject jsonObjItem = jsonArray
												.getJSONObject(i);

										String id = jsonObjItem.getString("id");
										alarmCodes.add(jsonObjItem.optString("region_code"));
										ids.add(id);
									}

								}
							} catch (JSONException e) {
								responseCode = HttpUtils.UNKNOWERROR;
							}

						}

					});

					httpUtils.sendRequest(requestUrl, null, "GET", "UTF-8",
							validateUsername, validatePassword, user);

					return ids;
				}

				@Override
				protected void onPostExecute(List<String> result) {

					customDialog.dismiss();

					if (customDialog != null) {

						filter = new JianGuanDuiXiangFilter();
						filter.setAll(false);
						filter.setIds(result);

						refreshData(getCurrentTown());

						if (navgationLayout != null) {
							navgationLayout.setVisibility(View.VISIBLE);
						}

					}

					super.onPostExecute(result);
				}

				@Override
				protected void onPreExecute() {

					customDialog = new CustomDialog(
							JianGuanDuiXiangActivity.this);

					customDialog.setMessage("正在加载警示数据..");

					customDialog.showWait();

					super.onPreExecute();
				}
			}.execute();

		}

		if (v == btnRight) {
			type=2;
			initNav();
			alarmCodes=new ArrayList<String>();
			edtFilterLinkMan.setText("");
			page=0;
			btnLeft.setBackgroundResource(R.drawable.btn_back_pressed);
			btnRight.setBackgroundResource(R.drawable.btn_back_pressed);

			filter = new JianGuanDuiXiangFilter();
			filter.setAll(true);
			page=0;
			refreshData(getCurrentTown());

			if (navgationLayout != null) {
				navgationLayout.setVisibility(View.VISIBLE);
			}

			btnRight.setBackgroundColor(getResources().getColor(
					R.color.title_color));
		}

		if (v == btnBack) {
			doBack();
		}

		if (v == btnFilter) {

			Intent intent = new Intent(JianGuanDuiXiangActivity.this,
					JianGuanDuiXiangFilterActivity.class);

			intent.putExtra("filter", filter);

			intent.putExtra("user", user);

			startActivityForResult(intent, REQUEST_FILTER_CODE);

		}

		if (v != null && v.getId() == R.id.llTongXuLuRightLayout) {

			final BeiJianDanWei beiJianDanWei = (BeiJianDanWei) v.getTag();

			if (CharsetUtil.isNotNull(beiJianDanWei.getTel())
					&& CharsetUtil.isNotNull(beiJianDanWei.getMobile())) {

				// 弹出对话框
				final CustomDialog customDialog = new CustomDialog(
						JianGuanDuiXiangActivity.this);

				customDialog.setOnActionClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						customDialog.dismiss();
					}
				});

				customDialog
						.setActionTxt(getString(R.string.jiandujiancha_item_cancle));
				customDialog
						.setTitle(getString(R.string.tongxunlu_btn_select_phone));

				ViewGroup view = (ViewGroup) getLayoutInflater().inflate(
						R.layout.tongxunlu_item_phone_select, null);

				// 个人电话
				final ViewGroup llPhone = (ViewGroup) view
						.findViewById(R.id.llPhone);

				TextView tvPhone = (TextView) llPhone
						.findViewById(R.id.tvTitle);

				tvPhone.setText(beiJianDanWei.getMobile());

				// 办公室电话
				final ViewGroup llHomeTel = (ViewGroup) view
						.findViewById(R.id.llHomeTel);

				TextView tvHomeTel = (TextView) llHomeTel
						.findViewById(R.id.tvHomeTel);

				tvHomeTel.setText(beiJianDanWei.getTel());

				customDialog.setView(view);
				customDialog.show();

				llPhone.setClickable(true);
				llHomeTel.setClickable(true);

				OnClickListener playPhoneClickListener = new OnClickListener() {

					@Override
					public void onClick(View v) {

						customDialog.dismiss();

						// 如果是办公室电话
						if (v == llHomeTel) {

							playPhone(beiJianDanWei.getTel());
						}

						// 如果是手机
						if (v == llPhone) {
							playPhone(beiJianDanWei.getMobile());
						}
					}
				};

				llPhone.setOnClickListener(playPhoneClickListener);
				llHomeTel.setOnClickListener(playPhoneClickListener);
			} else {

				String phone = "";

				if (CharsetUtil.isNotNull(beiJianDanWei.getTel())) {
					phone = beiJianDanWei.getTel();

				} else if (CharsetUtil.isNotNull(beiJianDanWei.getMobile())) {
					phone = beiJianDanWei.getMobile();
				}

				playPhone(phone);
			}
		}
	}

	/**
	 * 进行拨打电话的操作
	 *
	 * @param phone
	 */
	private void playPhone(String phone) {

		Intent intent_to_call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
				+ phone));

		startActivity(intent_to_call);
	}

	/**
	 * 刷新导航数据
	 */
	private void notifyDatasetNavgation() {

		navgationLayout.removeAllViewsInLayout();

		for (int i = 0; i <navgationDatas.size() ; i++) {
			Log.d("rafenav",navgationDatas.get(i).getName());
			TextView tvTextView = new TextView(this);
			tvTextView.setTextColor(getResources()
					.getColor(R.color.title_color));
			tvTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
			tvTextView.setTag(i);
			tvTextView.setBackgroundResource(R.drawable.btn_back_pressed);


			tvTextView.setSingleLine(true);
			tvTextView.setEllipsize(TruncateAt.END);

			int count=navgationDatas.get(i).getBeijiandanwei();
			tvTextView.setText(navgationDatas.get(i).getName()
					+ "\u0020[\u0020" +getAlarmCount(navgationDatas.get(i).getCode(),count)+ "\u0020]\u0020>>");

			tvTextView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					int position = (Integer) v.getTag();

					for (int j = navgationDatas.size() - 1; j > position; j--) {

						navgationDatas.remove(j);

					}
					page=0;
					refreshData(getCurrentTown());
				}

			});

			if (Long.parseLong(navgationDatas.get(i).getCode()) >= Long
					.parseLong(user.getKnowRegion())) {

				tvTextView.setClickable(true);
			} else {

				tvTextView.setClickable(false);
			}

			navgationLayout.addView(tvTextView, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position,
							long id) {

		Object data = adpJianGuanDuiXiang.getItem(position);

		if (data != null && data instanceof BeiJianDanWei) {

			Intent to_detail = new Intent(this, JianGuanDuiXiangDetail.class);

			BeiJianDanWei beiJianDanWei = (BeiJianDanWei) data;

			beiJianDanWei=db.queryBeiJianDanWie2(beiJianDanWei.getId(), beiJianDanWei.getCode());

			to_detail.putExtra("beiJianDanWei", beiJianDanWei);

			StringBuffer navgation = new StringBuffer();

			for (Town town : navgationDatas) {

				navgation.append(town.getName());

				navgation.append(">>");

			}

			String location = navgation.toString();

			if (location.length() > 0) {

				location = location.substring(0, location.length() - 2);
			}

			to_detail.putExtra("location", location);

			to_detail.putExtra("user", user);

			startActivity(to_detail);

		}

		if (data != null && data instanceof Town) {

			Town town = (Town) data;
			activeTown=town;
			page=0;
			refreshData(town);

		}

	}
	private Town activeTown=null;
	/**
	 * 获得当前的城镇
	 *
	 * @return
	 */
	private Town getCurrentTown() {

		Town town = navgationDatas.get(navgationDatas.size() - 1);

		return town;
	}

	@Override
	public void onBackPressed() {

		doBack();
	}

	/**
	 * 返回的时候
	 */
	private void doBack() {

		if (navgationDatas.size() == 1) {
			finish();
		} else {

			if (getCurrentTown().getCode().equals(user.getKnowRegion())) {
				finish();
			} else {
				page=0;
				navgationDatas.remove(navgationDatas.size() - 1);
				refreshData(getCurrentTown());
			}
		}

	}
	private int getAlarmCount(String code,int countdef){
		if(type==1){
			int count=0;
			for(int j=0;j<alarmCodes.size();j++){
				if(alarmCodes.get(j).indexOf(code)!= -1
						||alarmCodes.get(j).equals(code)){
					count++;
				}
			}
			return count;
		}else{
			return countdef;
		}


	}
	/**
	 * 刷新数据
	 *
	 * @param town
	 */
	private void refreshData(Town town) {

		// 		if (adpJianGuanDuiXiang != null) {
		// 			adpJianGuanDuiXiang.setFilter(filter);
		// 		}
		List datas = new ArrayList();
		if(page==0){
			List<Town> towns=db.queryTownByParent(town.getCode(),0);
			if(type==1){
				int all=0;
				for(int i=0;i<towns.size();i++){
					int count=0;
					for(int j=0;j<alarmCodes.size();j++){

						if(alarmCodes.get(j).indexOf(towns.get(i).getCode())!= -1
								||alarmCodes.get(j).equals(towns.get(i).getCode())){
							count++;
							all++;
						}
					}
					towns.get(i).setBeijiandanwei(count);
				}
				this.alarmSum=all;
			}

			//		   if(town.getCode().length()<8){
			for(int k=0;k<towns.size();k++){
				if(towns.get(k).getBeijiandanwei()>0){
					datas.add(towns.get(k));
				}
			}
			//		   }
			//			   }else{
			//				   datas.addAll(towns);
			//			   }
			datas.addAll(db.queryAllBeiJianDanWie(town.getCode() + "", this.filter,0,null,null));
		}else {
			try{
				List check=db.queryAllBeiJianDanWie(town.getCode() + "", this.filter,page,null,null);
				datas.addAll(check);
			}catch(java.lang.Exception e){
				e.printStackTrace();
			}
		}
		if (!navgationDatas.contains(town)) {
			navgationDatas.add(town);
		}

		notifyDatasetNavgation();
		if (datas.size() > 0) {
			hasNoData=true;
			if (adpJianGuanDuiXiang == null) {
				adpJianGuanDuiXiang = new JianGuanDuiXiangListAdapter2(
						JianGuanDuiXiangActivity.this, datas,
						JianGuanDuiXiangActivity.this, this.filter);

				lvDuiXiang.setAdapter(adpJianGuanDuiXiang);
			} else {

				adpJianGuanDuiXiang.setData(datas);

				adpJianGuanDuiXiang.notifyDataSetChanged();
			}
		} else {
			hasNoData=true;
			Toast.makeText(this, "没有更多数据 ", Toast.LENGTH_SHORT).show();
			datas.clear();
			if (adpJianGuanDuiXiang == null) {

				adpJianGuanDuiXiang = new JianGuanDuiXiangListAdapter2(
						JianGuanDuiXiangActivity.this, datas,
						JianGuanDuiXiangActivity.this, this.filter);
				lvDuiXiang.setAdapter(adpJianGuanDuiXiang);
			}
			adpJianGuanDuiXiang.setData(datas);
			adpJianGuanDuiXiang.notifyDataSetChanged();

		}

	}
	/**xrf
	 @Override
	 public void onLocationChanged(Location location) {
	 // TODO Auto-generated method stub

	 }

	 @Override
	 public void onProviderDisabled(String provider) {
	 // TODO Auto-generated method stub

	 }

	 @Override
	 public void onProviderEnabled(String provider) {
	 }

	 @Override
	 public void onStatusChanged(String provider, int status, Bundle extras) {
	 }
	 @Override
	 public void onLocationChanged(AMapLocation location) {

	 this.location = location;

	 if (adpJianGuanDuiXiang != null) {
	 adpJianGuanDuiXiang.setLocation(location);
	 }

	 }
	 **/
	@Override
	protected void onResume() {
		super.onResume();

		/*
		 * 初始化高德地图的定位对象
		 * xrf

		 mAMapLocManager = LocationManagerProxy.getInstance(this);

		 * mAMapLocManager.setGpsEnable(false);//
		 * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true Location
		 * API定位采用GPS和网络混合定位方式
		 * ，第一个参数是定位provider，第二个参数时间最短是5000毫秒，第三个参数距离间隔单位是米，第四个参数是定位监听者
		 
		 mAMapLocManager.requestLocationUpdates(
				LocationProviderProxy.AMapNetwork, 1500, 10, this);
*/
	}

	@Override
	protected void onPause() {
		super.onPause();
		/*
		 
		 * xrf
		if (mAMapLocManager != null) {
            mAMapLocManager.removeUpdates(this);
			mAMapLocManager.destory();

		}
		mAMapLocManager = null;
   */
	}



	private void refreshDataSelect(final String key) {



		navgationLayout.setVisibility(View.GONE);
		new Thread(new Runnable() {
			@Override
			public void run() {

				List datas = new ArrayList();

				UpdateDateUtilEx dataUtil = new UpdateDateUtilEx(db, JianGuanDuiXiangActivity.this, user);

				JSONObject data=dataUtil.searchEntOnline(key,page);
				if(data!=null){
					JSONArray list=data.optJSONArray("result");
					List<BeiJianDanWei> bjdsw=new ArrayList<BeiJianDanWei>();
					if(list!=null){
						for(int i=0;i<list.length();i++){
							JSONObject item=list.optJSONObject(i);
							BeiJianDanWei newb=new BeiJianDanWei();
							newb.setId(item.optString("id"));
							newb.setName(item.optString("name"));
							newb.setCode(item.optString("region_code"));

							bjdsw.add(newb);
						}
					}
					datas.addAll(bjdsw);

				}
				Message msg=new Message();
				msg.what=12;
				msg.obj=datas;
				mHandler.sendMessage(msg);

			}
		}).start();
	}
}
